Getting Started with the Solana CLI

·

As part of your Solana development journey, it's essential to familiarize yourself with the Solana Command Line Interface (CLI). Mastering a set of specific commands for executing various tasks is both powerful and critically important for effective blockchain development.

Why the Solana CLI Matters

The Solana CLI is your primary tool for interacting directly with the Solana blockchain. It enables developers to perform key operations such as managing wallets, funding accounts, deploying programs, and configuring network settings. Learning these commands forms the foundation of building and operating decentralized applications on this high-performance network.

Whether you're a beginner starting out or an experienced developer, understanding these command-line tools provides fine-grained control over your development workflow and blockchain interactions.

Creating a Solana Wallet for Development

To deploy Solana programs to the blockchain—whether to a local validator or remote RPC—you need to create a Solana wallet. For simplicity, we'll create a file system wallet that stores your private key in a file on your computer. This approach allows easy configuration for both Solana CLI and Anchor CLI to use this wallet when deploying programs.

The default file system wallet is typically located at ~/.config/solana/id.json. When creating new file system wallets, you can specify any particular file location using the --outfile /path/to/file flag.

Create a new Solana wallet with this command:

solana-keygen new --outfile ~/.config/solana/solfate-dev.json

During wallet creation, you can set a passphrase (similar to SSH key files).

I strongly recommend setting a wallet passphrase, especially for any wallets you might use in production environments!

After creating your wallet, you'll receive a readout of your "public key" (your wallet address) and seed phrase. If you plan to actually use this wallet address, don't forget to write down your seed phrase and store it securely.

Reading Your Solana Configuration

To accomplish anything on the Solana blockchain, even with a local test validator, you need to ensure your Solana CLI is configured to connect to the correct Solana RPC network and the appropriate wallet.

To view your current Solana CLI settings, run this command:

solana config get

You should receive output similar to:

Config File: /home/nick/.config/solana/cli/config.yml
RPC URL: https://api.testnet.solana.com
WebSocket URL: wss://api.testnet.solana.com/ (computed)
Keypair Path: /home/nick/.config/solana/id.json
Commitment: confirmed

The most important settings are the "RPC url" and "Keypair Path":

Updating Your Solana Configuration Settings

When you need to change your Solana network settings or modify the default wallet key file, you'll use the Solana CLI to update configuration settings.

To set your RPC url (for different Solana networks), you can use network shorthand (like "mainnet", "testnet", etc.) or the full URL of an active RPC node:

solana config set --url testnet

To change the wallet keyfile the CLI is using:

solana config set --keypair /path/to/file

Checking Your Wallet Balance

You'll need SOL to interact with the blockchain, even during development. You can easily check your (or anyone's) wallet balance at any time using the Solana CLI.

To check the balance using the wallet address configured in your CLI (the one you set using your file system keyfile):

solana balance

To check another wallet's balance:

solana balance WALLET_ADDRESS_HERE

The solana balance command will only check the balance for the given wallet on the currently selected Solana network.

Funding Your Wallet (Via Airdrop)

After setting up your new wallet and selecting the correct RPC network, you need to fund your wallet with SOL. This SOL will be used to deploy programs to the blockchain, as well as pay for rent and transaction fees while you develop your applications.

You'll need to regularly fund your wallet with SOL (the native Solana token) when deploying and interacting with Solana programs.

You can fund your wallet account using two primary methods:

Solana CLI Airdrop

Request a free airdrop to your currently selected wallet:

solana airdrop 1

The solana airdrop command has limitations on how much SOL can be airdropped. Any request exceeding this limit will cause the transaction to fail. At the time of writing, the limit is 2 SOL per request.

As you might have guessed, the airdrop will occur on the network you've selected in your Solana configuration settings. No, airdrops don't work on mainnet.

Public Faucet Airdrops

Another straightforward method to obtain free devnet or testnet SOL airdrops to your wallet is using public faucets. These services provide development SOL for testing purposes without real monetary value.

👉 Explore more strategies for obtaining test tokens

Running a Solana Test Validator

As mentioned earlier, when developing Solana programs locally, you'll likely want to run a localhost version of the Solana blockchain called a "test validator." This allows you to work with a full version of the Solana blockchain directly on your local machine.

Running and developing with a test validator is excellent (and highly recommended) for several reasons:

To run a Solana test validator, open a new terminal window and run this command:

solana-test-validator

This localhost network (aka "localnet") running in your terminal will output all messages from Solana programs to the blockchain. This provides an excellent method for debugging your programs during development.

Deploying Solana Programs

Once you've set up your development environment, configured your wallet, and obtained test SOL, you're ready to deploy Solana programs. The deployment process involves compiling your program and uploading it to the blockchain using your funded developer wallet.

The basic deployment command structure is straightforward, though the exact parameters may vary depending on your specific program and development setup.

Frequently Asked Questions

What is the Solana CLI used for?
The Solana CLI is a command-line interface tool that allows developers to interact with the Solana blockchain. It enables wallet management, network configuration, transaction signing, program deployment, and various other blockchain operations without needing a graphical interface.

How do I switch between different Solana networks using the CLI?
You can switch between Solana networks using the solana config set --url command followed by the network name (mainnet, testnet, devnet) or a specific RPC endpoint URL. This change affects which blockchain your commands will interact with.

Is it safe to use file system wallets for development?
File system wallets are convenient for development purposes but should be used with caution. Always set a strong passphrase and never use development wallets with significant amounts of real SOL. For production applications, consider more secure key management solutions.

Why would I need to run a local test validator?
A local test validator allows you to develop and test Solana programs without connecting to public networks. This provides faster iteration, predictable network conditions, free testing SOL, and detailed program logs for debugging purposes.

Can I use the same wallet for both development and production?
It's strongly recommended to use separate wallets for development and production. Development wallets may have reduced security measures and could be exposed to greater risks during testing and experimentation phases.

What's the difference between devnet and testnet?
Devnet is typically used for early development and testing with frequent resets, while testnet provides a more stable environment closer to mainnet conditions. Both use valueless test SOL, but testnet generally has more network stability for rigorous testing.