Setting up V on Windows

Goal

If you read this article and follow the steps closely, you should end up with a well-equipped system on Windows that will make developing with V as easy as possible.

Overview

It’s tedious to navigate to several websites, download the executables for every tool we need, and install them separately. Unless they’re allowed to check for updates, the installation of newer versions must be done manually as well. We’ll use a simpler approach — using a command-line installer, specifically Scoop (https://scoop.sh) for installing and updating all our software from a single source.

Followed by Scoop, we’ll install several critical software components that we’ll be using frequently. These are: git, PowerShell Core, Windows Terminal, MinGW (WinLibs), and VS Code.

Next, we’ll move on to installing V from source with our newly acquired tools. We’ll finish up with a test project to ensure that everything is working as intended.

Installing Scoop

Scoop is a community project which allows installation of common software from the command-line. First, we navigate to https://scoop.sh/ and take note of the instructions that we need to copy and paste. We’ll need PowerShell to execute these commands, so open the Start Menu and search for PowerShell ISE. The option for Windows PowerShell ISE should appear.

Open Windows PowerShell ISE and paste in the following command:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This command will only need to be run once. It allows the user to install software through running remote scripts via Scoop. A dialog box will appear, letting you know about the implications. Choose ‘Yes to All’ and proceed.

Once complete, execute the next command in Windows PowerShell:

irm get.scoop.sh | iex

This is the command that actually runs the installer script for Scoop. Once the prompt appears again, we’re ready to install our necessary software! In order to make sure that we’ve installed Scoop properly, run the following command:

scoop help

The output should look like to this:

PS C:\Users\Mastering V> scoop help

    Usage: scoop <command> [<args>]

    Available commands are listed below.

    Type 'scoop help <command>' to get more help for a specific command.

    Command    Summary                                                                                       

    -------    -------                                                                                       

    alias      Manage scoop aliases                                                                          

    bucket     Manage Scoop buckets                                                                          

    cache      Show or clear the download cache                                                              

    cat        Show content of specified manifest. If available, `bat` will be used to pretty-print the JSON.

    checkup    Check for potential problems                                                                  

    cleanup    Cleanup apps by removing old versions                                                         

    config     Get or set configuration values                                                               

    create     Create a custom app manifest                                                                  

    depends    List dependencies for an app, in the order they'll be installed                               

    download   Download apps in the cache folder and verify hashes                                           

    export     Exports installed apps, buckets (and optionally configs) in JSON format                       

    help       Show help for a command                                                                       

    hold       Hold an app to disable updates                                                                

    home       Opens the app homepage                                                                        

    import     Imports apps, buckets and configs from a Scoopfile in JSON format                             

    info       Display information about an app                                                              

    install    Install apps                                                                                  

    list       List installed apps                                                                           

    prefix     Returns the path to the specified app                                                         

    reset      Reset an app to resolve conflicts                                                             

    search     Search available apps                                                                         

    shim       Manipulate Scoop shims                                                                        

    status     Show status and check for new app versions                                                    

    unhold     Unhold an app to enable updates                                                               

    uninstall  Uninstall an app                                                                              

    update     Update apps, or Scoop itself                                                                  

    virustotal Look for app's hash or url on virustotal.com                                                  

    which      Locate a shim/executable (similar to 'which' on Linux)

Installing Git and updating PowerShell

One critical component that we’ll always need is a software called ‘git’. We’re not going to install it from their website, but rather, we’ll use Scoop again. If you want more information on Git and also good tutorials on how to use it, we recommend checking out their documentation: https://www.git-scm.com/doc

The version of PowerShell that ships with Windows is outdated. Every time you open a new ‘pwsh’ shell, you’ll be greeted with an update notice. In order to save time and avoid manually installing the latest version of PowerShell, we’ll install it through Scoop as well. To do so, execute the following command:

scoop install git pwsh

Here, we’re installing two things in one command. Scoop accepts one or more software names after the ‘install’ part of your command.

Once the output stops and we’re shown the command prompt again, verify that you have both programs properly installed by running these commands:

git --version
pwsh --version

Neither of them should throw an error, but instead, return a version number such as ‘git version 2.x.y…’ and ‘PowerShell 7.x.y’. Your output will vary, but you don’t need to worry about the specific version of these programs for now. Another nice feature about Scoop is that it makes it easy to update all the software we’ve installed through it in a single command.

Better Developer Experience with Windows Terminal and VS Code (Optional)

We’re almost ready to start developing in Windows. However, we’d like the experience to be uniform and pleasant for everyone, so here are some optional steps that you can follow.

First, we’ll install Windows Terminal, which provides a robust, customizable terminal for Windows developers. Previously, developers had to resort to third-party terminals, many of which are still excellent options today. But, seeing Microsoft put more effort towards improving developer experience is a breath of fresh air, so we’ll go ahead and use their official terminal.

The next software also comes from Microsoft, and it’s called ‘Visual Studio Code’. It’s the spiritual successor of the Atom editor from GitHub and has now become the most popular editor among all developers.

Let’s install both of them with the following two commands. Please note that the 2nd command below will cause Windows to prompt you for permission to make changes; go ahead and allow this permission when prompted:

scoop bucket add extras
scoop install vscode windows-terminal extras/vcredist2022

The first command adds the ‘extras’ bucket of software to Scoop’s search database. The next one is an install command, like we’ve seen before. Once both are installed, we’ll need to make a few adjustments to our Windows Terminal configuration, as shown in the next paragraph below. We also install ‘extras/vcredist2022’ because ‘windows-terminal’ recommends it in the post-install instructions.

Remember when we installed PowerShell Core (updated version) through Scoop? It doesn’t open as the default shell when we open Windows Terminal for the first time. Let’s fix that. Click on the down arrow next to the new tab ‘plus’ (+) button. Then click on Settings. Alternatively, press ‘Ctrl + ,’ to open the Settings page directly.

Click on ‘Windows PowerShell’ on the left pane. Expand the Command line option with the down arrow. Then click on Browse.

A dialog will appear that will let us choose the executable we want to use for the profile. Navigate to C:\Users<Your username>\scoop\apps\pwsh\current (make sure to substitute the placeholder text of <Your username> with your actual username). Click on the pwsh.exe executable and click Open.

After that, save the configurations and restart Windows Terminal. You should now get access to the latest ‘PowerShell Core. Some users have reported that Windows Terminal may occasionally crash after clicking ‘Open`. If this happens, simply search for ‘Terminal’ in your Start Menu again and click the menu item to re-open the program; our changes will have taken effect.

We’ll configure VS Code in another tutorial. For now, we’re missing one more piece of software that we need to install V.

Installing an updated C Compiler

The main V backend transforms V code to C and uses either the default TCC compiler that ships with V, or an external compiler like GCC, MSVC, or Clang, whichever it can find on the PATH. To learn about what a compiler is, this link is a good resource to get basic grounding on the concept. This link also gives an overview of the differences between a few major C compilers. For this tutorial, we’re going to also install the GCC compiler because it’s needed for compiling V production builds, as GCC binaries are better-optimized and have better output size.

If you want a different variant, use the search box on Scoop’s website and look for the term winlibs in the package names. Verify that your installation works by running these commands:

make --version
tcc -v

Run the following command to install GCC:

scoop install mingw-winlibs

Verify that your installation works by running these commands:

make --version
gcc --version

If you see text showing a version number or which says things about the copyright, such as ‘there is NO WARRANTY [etc…]’, then that means we’ve installed it correctly! These commands simply print version information for the programs we just installed, as a test to make sure they actually work.

We finally get to install V!

We have all the components in place. Now we can install V from source:

  1. Open Windows Terminal and click the dropdown arrow at the top next to the + plus icon, and click Windows PowerShell to open a new PowerShell tab.

  2. Create a new folder to install V in. We’ll make a folder called projects and a subfolder called vlang to store our V projects in. To do this, execute the following command:

    mkdir projects/vlang
  3. Navigate to the folder by running this command:

    cd projects/vlang
  4. Clone the V repository by executing the following command:

    git clone --depth 1 https://github.com/vlang/v.git

    This creates a new folder v with all the source code and the last commit in the local history (to save some bandwidth and disk space).

  5. Navigate into this directory by running: cd v

  6. Run .\make.bat (which will compile V on Windows).

  7. Run .\v symlink to add V to the path. This will take effect the next time you open Windows Terminal.

  8. Close and restart Windows Terminal.

  9. Verify that V works by running: v version

Yay! We now have V installed on our system. Let’s make a quick project and ensure everything works as expected.

A Test Run

We’re going to create a hello_world project to test V and make sure we can create programs successfully.

First, navigate into our projects folder again with the following command:

cd ~/projects/vlang

Next, run the command:

v new hello_world

When prompted for details about the version number and other project information, just leave these fields blank and press Enter. You can change these values later if you want to, by editing the v.mod file in the new hello_world project folder.

Navigate into the newly created project folder by running the following command in the Terminal:

cd hello_world

After this, run the following command:

v run .\hello_world.v

You should be greeted with a classic hello world message!

To perform one more compilation test, run the following command:

v -prod .

This command may take a little while to run, as it’s using GCC instead of the default TCC compiler. It will generate an executable with the same name as the project, i.e., .\hello_world.exe. Run .\hello_world.exe in the command line and you should see the same result.

Conclusion

And we’re done! You just set up a developer environment on Windows from scratch and ran some V code with it. In the next article, we will describe how to set up VS Code with the proper extensions and such.

If you run into any problems, feel free to reach out to us at [email protected] or our contact page.

Leave a Comment

Your email address will not be published. Required fields are marked *